/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-03 14:14:47
 * Copyright (c) 2015- joysuch-版权所有
 */
package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.BpStorageTankAreaAddBean;
import com.joysuch.wwyt.bp.bean.BpStorageTankAreaBean;
import com.joysuch.wwyt.bp.service.BpStorageTankAreaService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@Api(tags = "设备设施管理-罐区")
@RequestMapping("/bp/storage")
public class BpStorageTankAreaController {


    @Autowired
    private BpStorageTankAreaService bpStorageTankAreaService;

    @PostMapping("add")
    @RequiresPermissions("bp:storage:save")
    public ResultBean add(@RequestBody @Valid BpStorageTankAreaAddBean entity) {
        String errorMessage = bpStorageTankAreaService.add(entity);
        if (StringUtils.isNotBlank(errorMessage)) {
            throw new IllegalArgumentException(errorMessage);
        }
        return ResultBean.success("");
    }

    @RequestMapping("update")
    @RequiresPermissions("bp:storage:save")
    public Object update(@RequestBody @Valid BpStorageTankAreaAddBean s) {
        if (s.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        String errorMessage = bpStorageTankAreaService.update(s);
        if (StringUtils.isNotBlank(errorMessage)) {
            throw new IllegalArgumentException(errorMessage);
        }
        return ResultBean.success("");
    }

    @RequestMapping("page")
    public ResultBean page(Pageable pageable) {
        pageable = PageDataUtils.addOrderByDesc(pageable, "id");
        return bpStorageTankAreaService.findByPage(pageable);
    }

    @GetMapping("getById")
    public Object getById(Long id) {
        BpStorageTankAreaBean byId = bpStorageTankAreaService.findById(id);
        return ResultBean.wrapSuccessfulResult(byId);
    }

    @GetMapping("deleteById")
    @RequiresPermissions("bp:storage:delete")
    public ResultBean deleteById(Long id) {
        bpStorageTankAreaService.deleteById(id);
        return ResultBean.success("");
    }


}
